package com.example.administrator.newxlmedical;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.baseproject.BaseActivity;

import beans.NewUserInfoBean;
import butterknife.BindView;
import butterknife.ButterKnife;
import constants.Constants;
import constants.HandlerConstant;
import customhandler.MyHandler;
import singleton.SingletonClass;
import utils.ActivityControl;
import utils.ActivityUtil;
import utils.ObjectSaveUtil;

public class ScreensaverActivity extends BaseActivity {

    @BindView(R.id.tv_screensaver_user)
    TextView tvUser;

    @BindView(R.id.rl_screensaver)
    RelativeLayout rlScreensaver;
    @BindView(R.id.iv_screen_user_icon)
    ImageView ivIcon;
    @BindView(R.id.tv_screensaver_dept)
    TextView tvDept;
    @BindView(R.id.tv_screensaver_hint)
    TextView tvHint;

    private NewUserInfoBean.DataBean userInfoBean;
    private MyHandler allHandler = SingletonClass.getSingleton(MyHandler.class);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screensaver);
        ButterKnife.bind(this);
        if (!Constants.IS_FUYI) {
            // 重置 快捷开门 判断值
            Constants.IS_SHOW_FASTER_OPENDOOR = true;
            // 重置 快捷开门 语音 判断值
            Constants.IS_FRIST_OPENDOOR = true;
            allHandler.sendEmptyMessageDelayed(HandlerConstant.NO_OPERATION_EXIT, Constants.EXIT); // 退出登录
        }
        userInfoBean = (NewUserInfoBean.DataBean) ObjectSaveUtil.readObject(this, Constants.USER_INFO_BEAN, Constants.USER_INFO_BEAN);

        if (userInfoBean.getRoleInfoId() == Constants.ROLE_NURSE) { //护士
            ivIcon.setImageResource(R.mipmap.hushi);
        } else if (userInfoBean.getRoleInfoId() == Constants.ROLE_HEAD_NURSE) { //护士长
            ivIcon.setImageResource(R.mipmap.hushizhang);
        } else if (userInfoBean.getRoleInfoId() == Constants.ROLE_MANAGE) { //高层管理
            ivIcon.setImageResource(R.mipmap.gaocengguanli);
        } else if (userInfoBean.getRoleInfoId() == Constants.ROLE_FACTORY) { //供应商
            ivIcon.setImageResource(R.mipmap.gongyingshang);
        } else { //厂商
            ivIcon.setImageResource(R.mipmap.gongyingshang);
        }

        tvUser.setText(userInfoBean.getName());

        tvDept.setText(userInfoBean.getDepartment());


        getAlphaAnimotion();

        rlScreensaver.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtil.jumpToActivity(ScreensaverActivity.this, MainActivity.class);
                finish();
            }
        });
    }

    @Override
    protected void onHandleMessage(Message msg) {
        super.onHandleMessage(msg);
        switch (msg.what) {

            case HandlerConstant.NO_OPERATION_EXIT:
                ActivityUtil.jumpToActivity(ScreensaverActivity.this, EnterActivity.class);
                finish();
                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        allHandler.removeMessages(HandlerConstant.NO_OPERATION_EXIT);
    }

    /**
     * 渐变透明动画的实现
     */
    public void getAlphaAnimotion() {
        /**
         * 创建 AlphaAnimation 对象 并指定透明渐变的范围
         */
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f, 1.0f);
        /**
         * 设置动画执行的时间
         */
        alphaAnimation.setDuration(1500);
        /**
         * 设置动画是否在壁纸上运行
         */
        alphaAnimation.setDetachWallpaper(false);
        /**
         * 设置动画执行完毕后所要停留的位置
         */
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setFillEnabled(true);
        alphaAnimation.setFillBefore(true);
        /**
         * 设置动画为线性匀速执行
         */
        alphaAnimation.setInterpolator(new LinearInterpolator());

        /**
         * 设置动画执行重复的次数
         */
        alphaAnimation.setRepeatCount(Animation.INFINITE);
        /**
         * 设置动画执行重复的模式
         */
        alphaAnimation.setRepeatMode(Animation.REVERSE);

        tvHint.startAnimation(alphaAnimation);

    }

}
